// 计算最大值
export function calMax(arr) {
  let max = Math.max(...arr);
  let maxint = Math.ceil(max / 9.5);
  let maxval = maxint * 10;
  if (maxval == 0 || max < 1) {
    maxval = 1;
  }
  return maxval;
}

// 计算最小值
export function calMin(arr) {
  let min = Math.min(...arr);
  let minint = Math.floor(min / 10);
  let minval = minint * 10;
  return minval;
}

// 系列相关
export function getSeri({ type, name, color, ...others }) {
  let config = {
    type,
    name,
    itemStyle: { color },
  };
  if (type === 'line') {
    config = {
      ...config,
      symbol: 'none',
      lineStyle: { width: 1 },
    };
  } else if (type === 'bar') {
    config = {
      barMaxWidth: 20,
      ...config,
    };
  }
  return {
    ...config,
    ...others,
  };
}

// 基础配置
export const getOptions = ({ titleText, remSize, XAXIS, yAxis, grid, legendData }) => {
  return {
    title: {
      text: titleText,
      show: true,
      top: 8 * remSize,
      left: -4 * remSize,
      textStyle: {
        fontSize: 14 * remSize,
        fontWeight: 300,
        lineHeight: 14 * remSize,
        color: '#FFFFFF',
      },
    },
    grid: grid || {
      top: 46 * remSize,
      left: 46 * remSize,
      bottom: 46 * remSize,
      right: 36 * remSize,
    },
    tooltip: {
      trigger: 'axis',
      padding: [12 * remSize, 17 * remSize],
      formatter: (params) => {
        let res = '';
        params.forEach((item) => {
          let temp = `<span style=\"display:flex;align-items: center;justify-content: space-between;\">
          <span style=\"width:85px\">${item.marker}${item.seriesName}</span><span>`;
          if (item.seriesName === '电量') {
            temp += `${parseFloat(item.data).toFixed(3)}` + '<br/>';
          } else {
            temp += `${parseFloat(item.data).toFixed(2)}` + '<br/>';
          }
          temp += `</span></span>`;
          res = temp + res;
        });
        return params[0].name + '<br/>' + res;
      },
      position: function (point, params, dom, rect, size) {
        let x = 0;
        let y = 0;
        const pointX = point[0];
        const pointY = point[1];
        const boxWidth = size.contentSize[0];
        const boxHeight = size.contentSize[1];
        if (boxWidth > pointX) {
          x = pointX + 15;
        } else {
          x = pointX - boxWidth - 15;
        }
        if (boxHeight > pointY) {
          y = 5;
        } else {
          y = pointY - boxHeight;
        }
        return [x, y];
      },
      show: true,
      backgroundColor: '#161A1D',
      borderColor: '#161A1D',
      borderWidth: 1,
      textStyle: {
        color: '#A7A8A9',
        fontFamily: 'PingFang SC',
        fontStyle: 'none',
      },
    },
    legend: {
      right: 'center',
      top: 'bottom',
      width: '100%',
      itemGap: 13,
      itemWidth: 16,
      itemHeight: 8,
      textStyle: {
        color: 'white',
        fontSize: 12 * remSize,
        height: 14 * remSize,
        lineHeight: 17 * remSize,
      },
      padding: 0,
      data: legendData,
      inactiveColor: '#545454',
      inactiveBorderColor: '#545454',
      inactiveBorderWidth: 'inherit',
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: XAXIS.map((v) => v || ''),
      splitNumber: Math.random(),
      triggerEvent: true,
      boundaryGap: ['0%', '0%'],
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: 'rgba(24, 160, 251, 0.5)',
        },
      },
      axisLine: {
        show: true,
        onZero: false,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.4)',
        },
      },
      axisTick: {
        alignWithLabel: true,
        lineStyle: {
          color: '#4E5154',
        },
      },
      axisLabel: {
        // showMaxLabel: false,
        color: 'rgba(255,255,255,0.6)',
        fontStyle: 'DIN-Light',
        formatter: (value) => {
          const _label = value.split('');
          return _label.length > 10 ? value.substring(0, 9) + '...' : value;
        },
        // interval: Math.floor(XAXIS.length / 10),
        fontSize: 12 * remSize,
        lineHeight: 12 * remSize,
        margin: 9 * remSize,
      },
    },
    yAxis: yAxis || {
      type: 'value',
      axisLine: {
        show: false,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 12 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
  };
};
// 场站总览
export function DealElect(data = {}, remSize, isPower) {
  const { totalStatementQuantity = [], averagePrice = [], XAXIS = [] } = data;
  const yAxis = [
    {
      type: 'value',
      name: '电价(元/MWh)',
      axisLine: {
        show: false,
        onZeroAxisIndex: 1,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 14 * remSize,
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
    {
      type: 'value',
      name: isPower ? '电力(MW)' : '电量(MWh)',
      axisLine: {
        show: false,
        onZeroAxisIndex: 1,
      },
      axisTick: {
        alignWithLabel: true,
        length: 4,
        lineStyle: {
          color: '#737373',
        },
      },
      axisLabel: {
        color: '#6E6E6E',
        fontStyle: 'DIN-Light',
        fontSize: 12 * remSize,
        margin: 8 * remSize,
        height: 14 * remSize,
      },
      splitLine: {
        show: false,
        lineStyle: {
          type: 'solid',
          color: 'rgba(115,115,115,0.2)',
        },
      },
    },
  ];
  const legendData = [
    ,
    {
      name: isPower ? '电力' : '电量',
      icon: 'roundRect',
    },
    {
      name: '电价',
      itemStyle: { opacity: 1, borderWidth: 1, borderColor: '#F5BE15', color: '#272829' },
    },
  ];
  const option = getOptions({
    remSize,
    XAXIS,
    yAxis,
    legendData,
  });
  return {
    ...option,
    series:
      // XAXIS.length === 1
      //   ?
      [
        getSeri({
          name: '电价',
          type: 'line',
          symbol: 'circle',
          data: averagePrice,
          color: '#F5BE15',
          lineStyle: { width: 1 },
        }),
        getSeri({
          name: isPower ? '电力' : '电量',
          type: 'bar',
          data: totalStatementQuantity,
          color: '#5B8FF966',
          yAxisIndex: 1,
          barCategoryGap: 0,
        }),
      ],
    // :
    // [
    //     getSeri({
    //       type: 'line',
    //       name: '电价',
    //       smooth: true,
    //       symbol: 'circle',
    //       color: '#F5BE15',
    //       data: averagePrice,
    //       // data: totalStatementEnergyPrice
    //     }),
    //     getSeri({
    //       name: '电量',
    //       type: 'line',
    //       symbol: 'circle',
    //       data: totalStatementQuantity,
    //       color: '#5B8FF966',
    //       yAxisIndex: 1,
    //       step: 'start',
    //       areaStyle: {},
    //       lineStyle: { width: 0, color: '#222629' },
    //     }),
    //   ],
  };
}
